#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "utils.h"
#include <websocketclient.h>

#include <QMessageBox>
#include <QTime>

using namespace ServerMsgViewer;

MainWindow::MainWindow(QWidget* parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    , wssClient(new WebSocketClient)
{
    ui->setupUi(this);
    connect(wssClient, &WebSocketClient::onConnected, this, &MainWindow::onConnected);
    connect(wssClient, &WebSocketClient::onDisConnected, this, &MainWindow::onDisconnected);
    connect(wssClient, &WebSocketClient::onMsgRecved, this, &MainWindow::onMsg);
    configBtn();
}

MainWindow::~MainWindow()
{
    delete ui;
    delete wssClient;
}

void MainWindow::configBtn()
{
    connect(ui->btnConnect, &QPushButton::clicked, [this]() {
        QString btnTitle = ui->btnConnect->text();
        if (btnTitle.compare("Disconnect") == 0 && wssClient != nullptr) {
            wssClient->close();
        } else if (btnTitle.compare("Connect") == 0) {
            this->connectToServer();
        }
    });
    connect(ui->btnSend, &QPushButton::clicked, [this]() {
        if (!wssClient || ui->btnConnect->text().compare("Connect") == 0) {
            QMessageBox::critical(this, "Err", "Pls Establish Connection Before Send Data !");
            return;
        }
        //
        QString willSendString = ui->txtWillSend->toPlainText();
        if(willSendString.size() == 0){
            QMessageBox::critical(this, "Err", "Cant Send Empty Data , Wana Send \" A Ping \" ?");
            return;
        }
        wssClient->sendMsg(willSendString);
    });
    connect(ui->btnPing,&QPushButton::clicked,[this](){
        if (!wssClient || ui->btnConnect->text().compare("Connect") == 0) {
            QMessageBox::critical(this, "Err", "Pls Establish Connection Before Ping !");
            return;
        }
        wssClient->ping();
    });
    connect(ui->btnClear,&QPushButton::clicked,[this](){
        ui->txtServerMsg->clear();
    });
}

void MainWindow::connectToServer()
{
    if (!wssClient) {
        return;
    }
    QStringList inputConnectionInfo = ui->leIpAddr->text().split(":");
    if (inputConnectionInfo.empty() || inputConnectionInfo.length() != 2) {
        QMessageBox::critical(this, "Err", "Warning! Format is \" ipAddress:port \", ErrMsg: \" Bad Input \"");
        return;
    }
    QString& inputIpAddress = inputConnectionInfo[0];
    if (inputIpAddress.isEmpty()) {
        QMessageBox::critical(this, "Err", "Ip Address And Port Both Are Required");
        return;
    }
    byte testIP[4];
    if (!ip2ByteLen4(inputIpAddress, testIP)) {
        QMessageBox::critical(this, "Err", "illegal IpAddr");
        return;
    }
    QString& inputPort = inputConnectionInfo[1];
    bool isInt = false;
    int inputPortInt = inputPort.toInt(&isInt);
    if (!isInt) {
        QMessageBox::critical(this, "Err", "Bad Remote \"Port\" !");
        return;
    }
    wssClient->openUrl(QString("wss://%1:%2").arg(inputIpAddress).arg(inputPortInt));
}

void MainWindow::onConnected()
{
    ui->btnConnect->setText("Disconnect");
}

void MainWindow::onDisconnected()
{
    ui->btnConnect->setText("Connect");
}

void MainWindow::onMsg(QString msg)
{
    QPlainTextEdit* txt = ui->txtServerMsg;
    txt->appendPlainText(QString(
                             "======time %1=======\n"
                             "msg ->\t%2"
                             "\n")
                             .arg(currentTime())
                             .arg(msg));
}
